package com.quanyan.place.biz.impl;

import com.quanyan.common.constants.Constants;
import com.quanyan.place.biz.IPlaceRedisService;
import com.quanyan.place.cache.PlaceRedisTemplate;
import com.quanyan.place.constants.PlaceConstants;
import com.quanyan.place.entity.apiresp.RespBizPlaceBaseInfo;
import com.quanyan.redisCluster.utils.RedisKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by jingliu on 16/6/20.
 */
public class PlaceRedisServiceImpl implements IPlaceRedisService{

    @Autowired
    PlaceRedisTemplate placeRedisTemplate;

    @Override
    public List<RespBizPlaceBaseInfo> getRedisBizPlaceInfos(Integer uid) {
        RedisKey redisKey = new RedisKey(Constants.PLACE_SYSTEM, uid + PlaceConstants.USER_COLLECT_REDIS_KEY);
        List list = this.placeRedisTemplate.hvals(redisKey.getRedisKey());
        if (CollectionUtils.isEmpty(list)) {
            return null;
        }
        return list;
    }

    @Override
    public boolean saveRedisBizPlaceInfo(Integer uid, RespBizPlaceBaseInfo respBizPlaceBaseInfo) {
        try {
            RedisKey redisKey = new RedisKey(Constants.PLACE_SYSTEM, uid + PlaceConstants.USER_COLLECT_REDIS_KEY);
            this.placeRedisTemplate.hset(redisKey.getRedisKey(), respBizPlaceBaseInfo.getId() + "", respBizPlaceBaseInfo);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    @Override
    public boolean delRedisBizPlaceInfo(Integer uid, Integer placeId) {
        try {
            RedisKey redisKey = new RedisKey(Constants.PLACE_SYSTEM, uid + PlaceConstants.USER_COLLECT_REDIS_KEY);
            byte[] ids = {placeId.byteValue()};
            this.placeRedisTemplate.hdel(redisKey.getRedisKey(), ids);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
